package com.web.mall.service.impl;

import com.web.mall.mapper.TbContentMapper;
import com.web.mall.pojo.TbContent;
import com.web.mall.pojo.TbContentExample;
import com.web.mall.service.ContentManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class ContentManagerServiceImpl implements ContentManagerService {
    @Autowired
    private TbContentMapper tbContentMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<TbContent> queryContent() {
        return tbContentMapper.selectByExample(null);
    }

    @Override
    public TbContent findContentById(Long id) {
        return tbContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean saveContent(TbContent content) {
//        System.out.println("11111"+content.getPic());
        //判断 brand id
        if(ObjectUtils.isEmpty(content.getId())){
            tbContentMapper.insert(content);
            //清空缓存redis
            redisTemplate.boundHashOps("content").delete(content.getCategoryId());
        }else {
            TbContent oldContent = tbContentMapper.selectByPrimaryKey(content.getId());
            //清除之前分类的广告缓存
            redisTemplate.boundHashOps("content").delete(oldContent.getCategoryId());

            tbContentMapper.updateByPrimaryKey(content);
            //清除缓存
            if(content.getCategoryId()!= oldContent.getCategoryId()){
                redisTemplate.boundHashOps("content").delete(content.getCategoryId());
            }
        }
        return true;
    }

    @Override
    public boolean deleteContent(Long id) {
        if(id!=null){
            TbContent tbContent = tbContentMapper.selectByPrimaryKey(id);
            redisTemplate.boundHashOps("content").delete(tbContent.getCategoryId());
            tbContentMapper.deleteByPrimaryKey(id);
            return true;
        }else {
            return false;
        }
    }


    /**
     * 首次访问时，如果redis没有数据，就访问数据库，把数据存到redis
     * 后续访问时，直接查询redis
     *
     *   哈希结构
     *        redis Key  content
     *                     field                   value
     *                     categoryid 1 (轮播广告)           List<TbContent>
     *                         2 （今日关注 ）               List<TbContent>
     *
     */

    @Override
    public List<TbContent> findByCategoryId(Long categoryId) {

        //加入缓存代码：
        List<TbContent> list = (List<TbContent>) redisTemplate.boundHashOps("content").get(categoryId);

        if(list==null){ //首次加载
            System.out.println("查询数据库-----------");
            TbContentExample example = new TbContentExample();
            TbContentExample.Criteria criteria = example.createCriteria();
            //有效广告
            criteria.andStatusEqualTo("1");

            criteria.andCategoryIdEqualTo(categoryId);
            //排序
            example.setOrderByClause("sort_order");

            list= tbContentMapper.selectByExample(example);

            redisTemplate.boundHashOps("content").put(categoryId,list);

        }else {
            System.out.println("从缓存中获取-----------------");
        }
        return list;
    }
}
